﻿/***************************************************/
/******** Author: Krzysztof Bednarczyk *************/
/******** License: LGPL                *************/
/******** http://bordeux.NET           *************/
/******** Part of XVweb project        *************/
/******** xvweb.googlecode.com         *************/
/***************************************************/
(function (jQuery) {
		jQuery.fn.extend({
				h5w : function (options, val) {
					if (options == "destroy") {
						jQuery(this).trigger('h5w.destroy');
						return this;
					};
					if (options == "getContent") {
						return jQuery(this).find(".h5w-content").html();
					};
					var options = jQuery.extend({
							onStart : function (teadasd) {
								return true;
							}, 
							onChange : function () {
								return false;
							}, 
							onUseButtons : function () {
								return false;
							}, 
							onIconClick : function (icon) {
								
							}, 
							onLoad : function (icon) {
								
							}, 
							onTextarea : function (text) {
								
								return text;
							}, 
							onVisual : function (text) {
								
								return text;
							}, 
							onSelected : function (obj, jTarget, event) {
								
							}, 
							onUnSelected : function (obj, jTarget, event) {
								
							}, 
							content : "", 
							functions : {
								
							}
							
						}, options);
					
					var functions = jQuery.extend({
							italic : function (main, icon) {
								
								document.execCommand('italic', null, false);
								return true;
							}, 
							bold : function (main, icon) {
								document.execCommand('bold', null, false);
								return true;
							}, 
							fontsize : function (main, icon) {
							
								document.execCommand("fontSize", false, 1);
								toSizeFont = 12;
								if (typeof(main) == 'number') {
									toSizeFont = main;
								}else{
									toSizeFont = $(icon).text();
								};
									jQuery(".h5w-content font[size]").removeAttr("size").css("font-size", toSizeFont+"px");
								return false;
							}, 
							underline : function (main, icon) {
								document.execCommand('underline', null, false);
								return true;
							}, 
							strike : function (main, icon) {
								document.execCommand('strikeThrough', null, false);
								return true;
							}, 
							subscript : function (main, icon) {;
								document.execCommand('subscript', null, false);
								return true;
							}, 
							superscript : function (main, icon) {
								document.execCommand('superscript', null, false);
								return true;
							}, 
							paragraph : function (main, icon) {
								document.execCommand('insertParagraph', null, false);
								return true;
							}, 
							listordered : function (main, icon) {
								document.execCommand('insertOrderedList', null, false);
								return true;
							}, 
							listunordered : function (main, icon) {
								document.execCommand('insertUnorderedList', null, false);
								return true;
							},
							inserthtml : function (main, icon) {
								document.execCommand('insertHTML', null, $(icon).html());
								return true;
							}, 
							hrule : function (main, icon) {
								document.execCommand('insertHorizontalRule', null, false);
								return true;
							},
							redo : function (main, icon) {
								document.execCommand('redo' , false, null);
								return false;
							},
							selectall : function (main, icon) {
								document.execCommand('SelectAll', false, null);
								return true;
							}, 
							undo : function (main, icon) {
								//alert($(main).find(".h5w-content")[0]);
								document.execCommand('undo' , false, null);
								return false;
							}, 
							createlink : function (main, icon) {
								document.execCommand('CreateLink', null, 'changeit');
								jQuery(main).find(".h5w-content a[href='changeit']").attr({
										href : prompt("Please URL", "http://"), 
										target : "_blank"
									});
								
								return true;
							}, 
							insertimage : function (main, icon) {
								
								document.execCommand('insertImage', null, 'changeit');
								
								jQuery(main).find(".h5w-content img[src='changeit']").each(function () {
										src = jQuery.trim(prompt("Please image URL", "http://"));
										alt = jQuery.trim(prompt("Please set alt", ""));
										
										if (!(new RegExp("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+jQuery").test(src))) {
											alert("URLS is not valid");
											jQuery(this).remove();
											return true;
										};
										jQuery(this).attr({
												src : src, 
												alt : alt
											});
										this.execCommand("enableObjectResizing", false, false);
										this.attachEvent("onresizestart", function (e) {
												e.returnValue = false;
											}, false);
									});
								
								return true;
							}, 
							unlink : function (main, icon) {
								document.execCommand('Unlink', false, null);
								return true;
							},
							fonttype : function (main, icon) {
								$(main).find(".h5w-fonttype").html($(icon).find("span").clone());
								document.execCommand('fontName', false, $(icon).find("span").css("font-family"));
								return true;
							}, 
							
							justifyright : function (main, icon) {
								document.execCommand('justifyRight', false, null);
								return true;
							}, 
							justifycenter : function (main, icon) {
								document.execCommand('justifyCenter', false, null);
								return true;
							}, 
							justifyleft : function (main, icon) {
								document.execCommand('justifyLeft', false, null);
								return true;
							}, 
							paste : function (main, icon) {
								document.execCommand('paste', false, null);
								return true;
							}, 
							copy : function (main, icon) {
								document.execCommand('copy', false, null);
								return true;
							}, 
							cut : function (main, icon) {
								document.execCommand('cut', false, null);
								return true;
							}, 
							
							indent : function (main, icon) {;
								document.execCommand('indent', null, false);
								return true;
							}, 
							outdent : function (main, icon) {;
								document.execCommand('outdent', null, false);
								return true;
							}, 
							removeformat : function (main, icon) {
								document.execCommand('removeformat', null, false);
								return true;
							}, 
							formatblock : function (main, icon) {
								document.execCommand('formatBlock', null, "<" + jQuery(icon).attr("href").substr(1) + ">");
								return true;
							}, 
							
							fontcolor : function (main, icon) {
								document.execCommand("ForeColor", false, "#FFF");
							
								if (typeof(main) == 'string') {
									fontToSetColor = main;
								}else{
									fontToSetColor =jQuery(icon).parent().find(".h5w-font-picker").css("background-color");
								};
									jQuery(".h5w-content font[color]").removeAttr("color").css("color", fontToSetColor);
						
								return true;
							}, 
							hilitecolor : function (main, icon) {
								if (typeof(main) == 'string') {
									document.execCommand("hiliteColor", false, main);
									return false;
								};
								document.execCommand("hiliteColor", false, jQuery(icon).parent().find(".h5w-hilitecolor-picker").css("background-color"));
								return true;
							}, 
							table : function (main, icon, width, height) {
								document.execCommand('insertHTML', false, tablepicker.generate(16, 16));
								return true;
							}, 
							onIconClick : function (icon) {
								
							}
							
						}, options.functions);
					var tablepicker = {
						generate : function (width, height, anchor) {
							var TableCreator = "<table class='h5w-table-creator'>";
							for (n = 0; n <= height; ++n) {
								TableCreator += "<tr>";
								for (zn = 0; zn <= width; ++zn) {
									if (typeof anchor == "undefined") {
										TableCreator += "<td>&nbsp;</td>";
									} else {
										TableCreator += "<td><a href='#' ></a></td>";
									};
								};
								TableCreator += "</tr>";
							};
							TableCreator += "<table>";
							return TableCreator;
						}, 
						onSelect : function () {
							
						}, 
						prepare : function (MainHandle) {
							var onCloseTablePicker = function (event) {
								if (!(jQuery(event.target).is('.h5w-tablepicker-area') || jQuery(event.target).parents('.h5w-tablepicker-area').length)) {
									jQuery(".h5w-tablepicker-area").hide("slow");
									jQuery(document.body).unbind("click", onCloseTablePicker);
								};
								return false;
							};
							IHandle = jQuery(this);
							
							jQuery(MainHandle).find(".h5w-tablepicker").click(function () {
									
									tablepicker.onSelect = eval(jQuery(this).data("h5w-onselect-function"));
									var position = jQuery(this).position();
									
									jQuery(MainHandle).find(".h5w-tablepicker-area").css({
											left : position.left + jQuery(this).width() + 5, 
											top : position.top + jQuery(this).height() + 5
										}).html(tablepicker.generate(16, 16, true)).show("slow").find("table").delegate('td', 'mouseover mouseleave', function (e) {
											if (e.type == 'mouseover') {
												THandle = jQuery(this).parents("table");
												THeight = jQuery(this).parent().index();
												TWidth = jQuery(this).index() + 1;
												for (n = 0; n <= THeight; ++n) {
													THandle.find("tr:eq(" + n + ")").find("td:lt(" + TWidth + ")").addClass("hover");
												};
											} else {
												jQuery(this).parents("table").find("*").removeClass("hover");
											};
										}).find("td a").click(function (event) {
											tablepicker.onSelect(MainHandle, IHandle, jQuery(this).parent().index(), jQuery(this).parent().parent().index());
											jQuery(this).parents(".h5w-tablepicker-area").hide("slow");
											jQuery(document.body).unbind("click", onCloseTablePicker);
											
											return false;
										});
									setTimeout(function () {
											jQuery(document.body).click(onCloseTablePicker);
										}, 5);
									return false;
								});;
							
						}
						
					}, 
					picker = {
						changebg : ".something", 
						color : '', 
						onChange : function (color) {
							return false;
						}, 
						refreshSwatch : function (main) {
							var red = jQuery(this).parents(".h5w-picker-area").find(".h5w-picker-red").slider("value"), 
							green = jQuery(this).parents(".h5w-picker-area").find(".h5w-picker-green").slider("value"), 
							blue = jQuery(this).parents(".h5w-picker-area").find(".h5w-picker-blue").slider("value"), 
							opacity = (jQuery(this).parents(".h5w-picker-area").find(".h5w-picker-opacity").slider("value") / 100);
							
							picker.color = 'rgba(' + red + ',' + green + ',' + blue + ',' + opacity + ')';
							jQuery(this).parents(".h5w-picker-area").find(".h5w-picker-swatch").css("background-color", picker.color);
							jQuery(picker.changebg).css("background-color", picker.color);
							picker.onChange(picker.color);
							return true;
						}, 
						prepare : function (MainHandle) {
							jQuery(MainHandle).find(".h5w-picker-red, .h5w-picker-green, .h5w-picker-blue, .h5w-picker-opacity").slider({
									orientation : "horizontal", 
									range : "min", 
									max : 255, 
									value : 127, 
									slide : picker.refreshSwatch, 
									change : picker.refreshSwatch
								});
							jQuery(MainHandle).find(".h5w-picker-red").slider("value", 255);
							jQuery(MainHandle).find(".h5w-picker-green").slider("value", 140);
							jQuery(MainHandle).find(".h5w-picker-green").slider("value", 140);
							jQuery(MainHandle).find(".h5w-picker-opacity").slider({
									max : 100, 
									value : 100, 
								});
							
							jQuery(MainHandle).find(".h5w-picker").click(function () {
									var position = jQuery(this).position();
									picker.changebg = jQuery(this).data("h5w-destination");
									picker.onChange = eval(jQuery(this).data("h5w-onchange-function"));
									jQuery(MainHandle).find(".h5w-picker-area").css({
											left : position.left + jQuery(this).width() + 5, 
											top : position.top + jQuery(this).height() + 5
										}).show("slow");
									var onClosePicker = function (event) {
										if (!(jQuery(event.target).is('.h5w-picker-area') || jQuery(event.target).parents('.h5w-picker-area').length)) {
											jQuery(".h5w-picker-area").hide("slow");
											jQuery(document.body).unbind("click", onClosePicker);
										};
										return false;
									};
									setTimeout(function () {
											jQuery(document.body).click(onClosePicker);
										}, 5);
									
								});;
							
						}
						
					};
					
					sizepicker = {
						changesize : ".something", 
						fontSize : 12, 
						onChange : function (fontSize) {
							return false;
						}, 
						refreshSize : function (main) {
							
							sizepicker.fontSize = (jQuery(this).parents(".h5w-fontsize-picker-area").find(".h5w-fontsize-picker-size").slider("value"));
							
							jQuery(sizepicker.changesize).text(sizepicker.fontSize);
							sizepicker.onChange(sizepicker.fontSize);
							return true;
						}, 
						
						prepare : function (MainHandle) {
							
							jQuery(MainHandle).find(".h5w-fontsize-picker").click(function () {
									sizepicker.changesize = jQuery(this).data("h5w-destination");
									
									jQuery(MainHandle).find(".h5w-fontsize-picker-size").slider("destroy").slider({
											max : 73, 
											min : 8, 
											value : $(sizepicker.changesize).text(), 
											slide : sizepicker.refreshSize, 
											change : sizepicker.refreshSize
										});
									
									var position = jQuery(this).position();
									sizepicker.changesize = jQuery(this).data("h5w-destination");
									sizepicker.onChange = eval(jQuery(this).data("h5w-onchange-function"));
									jQuery(MainHandle).find(".h5w-fontsize-picker-area").css({
											left : position.left + jQuery(this).width() + 5, 
											top : position.top + jQuery(this).height() + 5
										}).show("slow");
									var onCloseFontSizePicker = function (event) {
										if (!(jQuery(event.target).is('.h5w-fontsize-picker-area') || jQuery(event.target).parents('.h5w-fontsize-picker-area').length)) {
											jQuery(".h5w-fontsize-picker-area").hide("slow");
											jQuery(document.body).unbind("click", onCloseFontSizePicker);
										};
										return false;
									};
									setTimeout(function () {
											jQuery(document.body).click(onCloseFontSizePicker);
										}, 5);
									
								});;
							
						}
						
					};	
					typepicker = {
						changeType : ".something", 
						fontType : "Arial", 
						onChange : function (fontType) {
							return false;
						}, 
						
						prepare : function (MainHandle) {
							
							jQuery(MainHandle).find(".h5w-fonttype-picker").click(function () {
									typepicker.changeType = jQuery(this).data("h5w-destination");
									

									
									var position = jQuery(this).position();
									
									typepicker.changeType = jQuery(this).data("h5w-destination");
									typepicker.onChange = eval(jQuery(this).data("h5w-onchange-function"));
									jQuery(MainHandle).find(".h5w-font-picker-area").css({
											left : position.left + jQuery(this).width() - 100, 
											top : position.top + jQuery(this).height() + 10
										}).show("slow");
									var onCloseFontTypePicker = function (event) {
										if (!(jQuery(event.target).is('.h5w-font-picker-area') || jQuery(event.target).parents('.h5w-font-picker-area').length)) {
											jQuery(".h5w-font-picker-area").hide("slow");
											jQuery(document.body).unbind("click", onCloseFontTypePicker);
										};
										return false;
									};
									setTimeout(function () {
											jQuery(document.body).click(onCloseFontTypePicker);
										}, 5);
									
								});;
							
						}
						
					};
					
					function destroy() {
						
					};
					function selectElement(event) {
						var jTarget = $(event.target);
						if (jTarget.hasClass("h5w-element-selected")) {
							return false;
						};
						
						$(".h5w-element-selected").removeClass("h5w-element-selected");
						
						if (jTarget.parents('.h5w-content').length || jTarget.is(".h5w-content")) {
							jTarget.addClass("h5w-element-selected");
							FontFamily = jTarget.css("font-family");
							FontShortName = FontFamily.split(",");
							$(".h5w-fonttype span").css("font-family" , FontFamily).text(FontShortName[0]); // tutaj też się da lepiej ;]
							$(".h5w-fontsize").text(jTarget.css("font-size").replace("px", ""));  // tutaj też
							
							commandToExec = eval("options.onElement" + event.target.tagName.toUpperCase());
							if (jQuery.isFunction(commandToExec)) {
								commandToExec(this, jTarget, event);
							};
							options.onSelected(this, jTarget, event);
						};
						options.onUnSelected(this, jTarget, event);
						return true;
					};
					
					return this.each(function () {
							
							var MainHandle = this;
							jQuery(MainHandle).bind('h5w.destroy', destroy);
							
							jQuery(MainHandle).find('.h5w-tabs').tabs({
									
								}).find(".ui-tabs-nav").sortable({
									axis : "x"
								});
							jQuery(MainHandle).find(".h5w-icon").bind("click", function () {
									FunName = jQuery(this).data("h5w-function");
									ToUseFunction = eval("functions." + FunName);
									if (jQuery.isFunction(ToUseFunction)) {
										ToUseFunction(MainHandle, this);
									} else {
										alert("Not found function: " + FunName);
									};
									jQuery(MainHandle).find(".h5w-content").trigger("change");
									options.onUseButtons(this, FunName, ToUseFunction);
									return false;
								});
							jQuery(document).keydown(function (event) {
									if (event.keyCode == 9) {
										document.execCommand('insertHTML', false, "\t");
										event.preventDefault();
										event.returnValue = false;
									}
								});
							jQuery(MainHandle).find(".h5w-tabs-bottom").tabs();
							jQuery(MainHandle).find(".h5w-tabs-bottom .ui-tabs-nav, .h5w-tabs-bottom .ui-tabs-nav > *") 
							.removeClass("ui-corner-all ui-corner-top") 
							.addClass("ui-corner-bottom");
							jQuery(MainHandle).find(".h5w-refresh-textarea").click(function () {
									jQuery(MainHandle).find(".h5w-texarea").val(options.onTextarea(jQuery(MainHandle).find(".h5w-content").html()));
								});
							jQuery(MainHandle).find(".h5w-refresh-editor").click(function () {
									jQuery(MainHandle).find(".h5w-content").html(options.onVisual(jQuery(MainHandle).find(".h5w-texarea").val())).trigger("change");
									
								});
							jQuery(MainHandle).find(".h5w-texarea").val(options.onVisual(jQuery(MainHandle).find(".h5w-content").html()));
							picker.prepare(MainHandle);
							tablepicker.prepare(MainHandle);
							sizepicker.prepare(MainHandle);
							typepicker.prepare(MainHandle);
							
							jQuery(MainHandle).find(".h5w-content").html(options.content).bind("keyup change", options.onChange).click(selectElement);
							jQuery(MainHandle).find(".h5w-scroll").click(function () {
									StyleBox = jQuery(MainHandle).find(jQuery(this).data("h5w-toscroll"));
									
									StyleBox.animate({
											"scrollTop" : jQuery(this).data("h5w-scroll") 
										}, "slow");
								});
							
							jQuery(MainHandle).find(".h5w-content").trigger("change");
							options.onLoad(this);
						});
				}, 
				getContent : function () {
					jQuery(this).find(".h5w-refresh-textarea").trigger("click");
					return jQuery(this).find(".h5w-texarea").val().replace("<!--?php", "<?php");
				}, 
				setContent : function (cont) {
					jQuery(this).find(".h5w-content").html(cont).trigger("change");
				}, 
				editor : function () {
					return jQuery(this).find(".h5w-content");
				}
				
			});
	})(jQuery);
 